
package uestc.bitman.edxapp.mainui;

import uestc.bitman.edxapp.EdxApp;
import uestc.bitman.edxapp.EdxStudentUser;

import android.annotation.SuppressLint;
import android.os.AsyncTask;
import android.os.Handler;
import android.os.Message;

public class UserLoginTask extends AsyncTask<UserLoginListener, Void, Boolean> {

    private boolean isTimeout = false;

    private UserLoginListener mListener;

    @SuppressLint("HandlerLeak")
    private Handler handler = new Handler() {
        public void handleMessage(Message msg) {
            if (msg.what == 0) {
                isTimeout = true;
            }
        }
    };

    @Override
    protected Boolean doInBackground(UserLoginListener... params) {
        mListener = params[0];
        EdxStudentUser user = EdxApp.getEdxAppUser();
        boolean bLoginStatus = user.login(handler);
        return bLoginStatus;
    }
    
    protected void onPostExecute(final Boolean success) {
        if(isTimeout){
            mListener.onTimeOut();
        }
        else{
            mListener.onLoginResult(success);
        }
    }
    
    protected void onCancelled() {
        mListener.onCancel();
    }
}
